using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;
namespace Data
{
    public class PhieuThueRepository
    {



		//////////////////////////////////////////////////////////

		                                                          

		//            Enter code your here                      //
        public PhieuThueDTO LayPhieuThueHienTaiCuaPhong(int MaPhong)
        {
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var query = (from x in Context.PhieuThueList
                         orderby x.MaPhieuThue descending
                         select x).First();
            PhieuThueDTO pt = new PhieuThueDTO();
            if (query != null)
            {
                pt = PhieuThueRepository.CreatePhieuThueDTOFromPhieuThueEF(query);
            }
            return pt;
        }

		
		                                                          

		//////////////////////////////////////////////////////////

        public static PhieuThueDTO CreatePhieuThueDTOFromPhieuThueEF(PhieuThue objEf) // EF -> DTO
        {
            PhieuThueDTO objDTO = new PhieuThueDTO();
            Mapper.CreateMap<PhieuThue, PhieuThueDTO>();
            objDTO = Mapper.Map<PhieuThue, PhieuThueDTO>(objEf);
            return objDTO;
        }

        public static PhieuThue CreatePhieuThueEFFromPhieuThueDTO(PhieuThueDTO objDTO)    //DTO -> EF
        {
            PhieuThue objEF = new PhieuThue();
            Mapper.CreateMap<PhieuThueDTO, PhieuThue>();
            objEF = Mapper.Map<PhieuThueDTO, PhieuThue>(objDTO);
            return objEF;
        }

        public static int Count()  //Dem so luong cac bo du lieu trong bang
        {
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            int qr = (from x in Context.PhieuThueList
                      select x.MaPhieuThue).Count();
            return qr;
        }

        public int GetIdentity() // tra ve gia tri ma moi them vo( gia tri Ma lon nhat trong Bang).
        {
            try
            {
                 QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
                  var query = from x in Context.PhieuThueList
                        orderby x.MaPhieuThue descending
                        select x.MaPhieuThue;
                  var Ma = query.First();
                  return int.Parse(Ma.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public bool Them(PhieuThueDTO objDTO)
        {
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                PhieuThue objEF = CreatePhieuThueEFFromPhieuThueDTO(objDTO);
                context.AddToPhieuThueList(objEF);
                context.SaveChanges();
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

        public void CapNhat(PhieuThueDTO objDTO)
        {
            try
            {
                PhieuThue objEF = CreatePhieuThueEFFromPhieuThueDTO(objDTO);
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                context.PhieuThueList.ApplyCurrentValues(objEF);
                context.SaveChanges();
            }
            catch (System.Exception )
            {            	
            }
        }

        public List<PhieuThueDTO> LayDanhSach(int Start, int Record)//lay Record bo du lieu tu vi tri Start
        { 
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var query = (from x in Context.PhieuThueList
                        orderby x.MaPhieuThue
                        select x).Skip(Start - 1).Take(Record);
            List<PhieuThueDTO> lst = new List<PhieuThueDTO>();
            foreach (var obj in query)
            {
                PhieuThueDTO lp = PhieuThueRepository.CreatePhieuThueDTOFromPhieuThueEF(obj);
                lst.Add(lp);
            }
            return lst;
        }

        public List<PhieuThueDTO> LayDanhSach()//lay tat ca danh sach
        {
            List<PhieuThueDTO> lst = new List<PhieuThueDTO>();
            QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
            var query = from PhieuThue in context.PhieuThueList
                        select PhieuThue;
            foreach (var objEf in query)
            {
                PhieuThueDTO objDTO = CreatePhieuThueDTOFromPhieuThueEF(objEf);
                lst.Add(objDTO);
            }
            return lst;
        }

        public PhieuThueDTO LayTheoMa(int MaPhieuThue)
        {
            PhieuThueDTO objDTO = new PhieuThueDTO();
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                var query = from PhieuThue in context.PhieuThueList
                            where PhieuThue.MaPhieuThue == MaPhieuThue
                            select PhieuThue;
                PhieuThue objEF = query.First();
                objDTO = CreatePhieuThueDTOFromPhieuThueEF(objEF);
            }
            catch (System.Exception )
            {
            }
            return objDTO;
        }

//
//Khoa ngoai Phong
//
        public List<PhieuThueDTO> LayDanhSachTheoMaPhong( int MaPhong)
        {
            List<PhieuThueDTO> lst = new List<PhieuThueDTO>();
            try
            {
                 QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
                 var query = from PhieuThue in context.PhieuThueList
                         where PhieuThue.MaPhong == MaPhong 
                        select PhieuThue;
                 foreach (var objEf in query)
                 {
                      PhieuThueDTO objDTO = CreatePhieuThueDTOFromPhieuThueEF(objEf);
                       lst.Add(objDTO);
                 }
            }
            catch
            {}
            return lst;
        }



        public void XoaTheoMaPhong( int MaPhong, bool XoaKhoaNgoai=false)
        {
            List<PhieuThueDTO> lst = this.LayDanhSachTheoMaPhong( MaPhong);
            for( int i = 0; i < lst.Count; i++ )
            {
                 int iMa = lst[i].MaPhieuThue;
                 this.Xoa(iMa, XoaKhoaNgoai);
            }
        }

        public bool Xoa(int MaPhieuThue, bool XoaKhoaNgoai=false)
        {
            try
            {
                if( XoaKhoaNgoai == true)
                {
                
                       ChiTietHoaDonRepository ChiTietHoaDonRepo = new ChiTietHoaDonRepository();
                       ChiTietHoaDonRepo.XoaTheoMaPhieuThue(MaPhieuThue, XoaKhoaNgoai);

                       ChiTietPhieuThueRepository ChiTietPhieuThueRepo = new ChiTietPhieuThueRepository();
                       ChiTietPhieuThueRepo.XoaTheoMaPhieuThue(MaPhieuThue, XoaKhoaNgoai);

                }
                QuanLyKhachSanEntities context = new QuanLyKhachSanEntities();
                var query = from x in context.PhieuThueList
                            where x.MaPhieuThue == MaPhieuThue 
                            select x;
                if(query.First()!= null)
                {
                    context.DeleteObject(query.First());
                    context.SaveChanges();
                }
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

////////////////////////////////////////////////////////////////

    }
}

